package com.qfhd.soft.cjzcore.Handle;

import android.os.Handler;
import android.os.Message;

import com.qfhd.soft.cjzcore.Core.CJLogger;
import java.util.ArrayList;
import java.util.List;

public class CJHandle {
    private List<CJHandleObject> registers = new ArrayList<>();

    private static CJHandle handle;
    public static CJHandle getInstance(){
        if(handle == null){
            synchronized (CJHandle.class){
                handle = new CJHandle();
            }
        }
        return handle;
    }

    public void init(){

    }

    private boolean isRegister(CJHandleObject handleObject){
        for(CJHandleObject obj : registers){
            if(obj.type == handleObject.type){
                return true;
            }
        }
        return false;
    }

    public void register(CJHandleObject handleObject){
        if(!isRegister(handleObject)){
            registers.add(handleObject);
        }
    }

    public void unRegister(int type){
        for(CJHandleObject obj : registers){
            if(obj.type == type){
                registers.remove(obj);
                break;
            }
        }
    }

    public void sendmessage(CJHandelMessage message){
        //获取 handel
        CJHandleObject tmp = null;
        for(CJHandleObject obj : registers){
            if(obj.type == message.type){
                tmp = obj;
                break;
            }
        }

        if(tmp == null){
            CJLogger.v("message center not find。type:" + message.type);
        }
        else{
//            tmp.handler.sendEmptyMessage(message.what_type);
            Message message1 = new Message();
            message1.obj = message.object;
            message1.what = message.what_type;
            tmp.handler.sendMessage(message1);
        }
    }
}
